﻿Imports AvianWaves.AvianPlay.Language
Imports System.Windows.Forms

Public Class ErrorLog
  Private Shared _logPath As String = "\AvianPlayError.log"
  Private Shared _logLength As Integer = -1
  Private Shared _language As LanguageReader

  Public Shared WriteOnly Property LogPath() As String
    Set(ByVal value As String)
      _logPath = value
    End Set
  End Property

  Public Shared WriteOnly Property Language() As LanguageReader
    Set(ByVal value As LanguageReader)
      _language = value
    End Set
  End Property

  Public Shared WriteOnly Property MaxRows() As Integer
    Set(ByVal value As Integer)
      _logLength = value
    End Set
  End Property

  Public Shared Sub WriteToErrorLog(ByVal msg As String)
    SyncLock GetType(ErrorLog)
      ErrorLogCleanup()

      Dim fws As IO.FileStream = Nothing
      fws = New IO.FileStream(_logPath, IO.FileMode.Append, IO.FileAccess.Write, IO.FileShare.Read)
      Try
        Dim fw As System.IO.StreamWriter = Nothing
        fw = New System.IO.StreamWriter(fws)
        Try
          fw.WriteLine(Date.Now.ToString.Replace(" ", "+") & " " & System.Web.HttpUtility.UrlEncode(msg))
          fw.Flush()
        Finally
          If fw IsNot Nothing Then
            fw.Close()
            fw = Nothing
            fws = Nothing
          End If
        End Try
      Catch ex As Exception
        MessageBox.Show(_language.GetString("UnableToWriteErrorLog") & " " & ex.Message)
      Finally
        If fws IsNot Nothing Then
          fws.Close()
        End If
      End Try
    End SyncLock
  End Sub

  Private Shared Sub ErrorLogCleanup()
    If IO.File.Exists(_logPath) Then
      If _logLength > 0 Then
        Dim el As New Queue(Of String)
        Dim frs As IO.FileStream = Nothing
        Try
          frs = New IO.FileStream(_logPath, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
          Dim fr As System.IO.StreamReader = Nothing
          fr = New System.IO.StreamReader(frs)
          Try
            While Not fr.EndOfStream
              el.Enqueue(fr.ReadLine())
              If el.Count > _logLength Then
                el.Dequeue()
              End If
            End While
          Finally
            If fr IsNot Nothing Then
              fr.Close()
              fr = Nothing
              frs = Nothing
            End If
          End Try
        Catch ex As Exception
          MessageBox.Show(_language.GetString("UnableToTruncateErrorLog") & " [1] " & ex.Message)
        Finally
          If frs IsNot Nothing Then
            frs.Close()
          End If
        End Try

        Dim fws As IO.FileStream = Nothing
        fws = New IO.FileStream(_logPath, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
        Try
          Dim fw As System.IO.StreamWriter = Nothing
          fw = New System.IO.StreamWriter(fws)
          Try
            While el.Count > 0
              fw.WriteLine(el.Dequeue)
            End While
            fw.Flush()
          Finally
            If fw IsNot Nothing Then
              fw.Close()
              fw = Nothing
              fws = Nothing
            End If
          End Try
        Catch ex As Exception
          MessageBox.Show(_language.GetString("UnableToTruncateErrorLog") & " [2] " & ex.Message)
        Finally
          If frs IsNot Nothing Then
            Try : frs.Close() : Catch : End Try
          End If
          If fws IsNot Nothing Then
            Try : fws.Close() : Catch : End Try
          End If
        End Try
      End If
    End If
  End Sub
End Class
